﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySSO.Core
{
    public class HttpQueryStringHelper
    {
        /// <summary>
        /// Appends a parameter to the QueryString
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string GetAppendedQueryString(string Url, string Key, string Value)
        {
            if (Url.Contains("?"))
            {
                Url = string.Format("{0}&{1}={2}", Url, Key, Value);
            }
            else
            {
                Url = string.Format("{0}?{1}={2}", Url, Key, Value);
            }

            return Url;
        }

        /// <summary>
        /// 去除url参数
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string RemoveParm(string url, string param)
        {
            string url1 = url;
            if (url.IndexOf(param) > 0)
            {
                if (url.IndexOf("&", url.IndexOf(param) + param.Length) > 0)
                {
                    url1 = url.Substring(0, url.IndexOf(param) - 1) + url.Substring(url.IndexOf("&", url.IndexOf(param) + param.Length) + 1);
                }
                else
                {
                    url1 = url.Substring(0, url.IndexOf(param) - 1);
                }
                return url1;
            }
            else
            {
                return url1;
            }
        }

        public static string SetQueryString(string Url, string Key, string Value)
        {
            string resulturl = RemoveParm(Url, Key);
            return GetAppendedQueryString(resulturl, Key, Value);
        }
    }
}
